import { Request, Response, NextFunction } from 'express'

export const filter = async (
  req: Request,
  res: Response,
  next: NextFunction
) => {
  const { post, user, action } = req.query

  req.filterParmas = {
    name: 'default',
    sql: 'comment.parent_id IS NULL'
  }

  if (post && !(user || action)) {
    req.filterParmas = {
      name: 'postComments',
      sql: `comment.parent_id IS NULL AND comment.post_id = ?`,
      param: post as string
    }
  }

  if (user && action == 'published' && !post) {
    req.filterParmas = {
      name: 'userPublished',
      sql: `comment.parent_id IS NULL AND comment.user_id = ?`,
      param: user as string
    }
  }

  if (user && action == 'replied' && !post) {
    req.filterParmas = {
      name: 'userReplied',
      sql: `comment.parent_id IS NOT NULL AND comment.user_id = ?`,
      param: user as string
    }
  }

  next()
}
